﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace App
{
    public partial class ThreadList : System.Web.UI.Page
    {
        //ID teamu pro sestaveni fora
        int TeamID;

        AppLib.Team team;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Common.GetLoggedUser(this.Page) == null)
            {
                Response.Redirect("Login.aspx");
                return;
            }

            TeamID = int.Parse(Server.HtmlEncode(Request.Cookies["ThreadList"].Value));

            team = MidApp.ManageTeam.GetTeamByID(TeamID);
            ImageAvatar.DefaultImage = team.ImageURL;
            ImageAvatar.Email = team.TeamID.ToString();
            LinkButtonTeamInfo.Text = team.TeamName;
        }
        protected void Page_PreRender(object sender, EventArgs e)
        {
            if (MidApp.ManageIsAdministrator.IsAdministrator(Common.GetLoggedUser(this.Page).UserID))
            {
                RepeaterThreads.DataSource = MidApp.ManageTopic.GetThreadByAuthorAndTeam(TeamID, Common.GetLoggedUser(this.Page).UserID);
                RepeaterThreads.DataBind();
            }
            else
            {
                RepeaterThreads.DataSource = MidApp.ManageTopic.GetThreadsBelongingToTeam(TeamID);
                RepeaterThreads.DataBind();
            }
        }

        protected void ButtonNewTopic_Click(object sender, EventArgs e)
        {
            AppLib.Topic aux = new AppLib.Topic();
            aux.TeamID = TeamID;

            if (TextBoxNewTitle.Text.Length >= 250)
                aux.Title = TextBoxNewTitle.Text.Remove(250);
            else
                aux.Title = TextBoxNewTitle.Text;

            aux.Inserted = DateTime.Now;
            aux.UserID = Common.GetLoggedUser(this.Page).UserID;
            MidApp.ManageTopic.CreateNewTopic(aux);

            TextBoxNewTitle.Text = "";
            collapsiblePanelExtender.Collapsed = true;

            int ThreadID = MidApp.ManageTopic.GetThreadByNameAndDate(aux.Title, aux.Inserted).ThreadID;

            Response.Cookies["MessageList"].Value = ThreadID.ToString();
            Response.Redirect("MessageList.aspx");
        }
        protected void LinkbuttonThread_Click(object sender, EventArgs e)
        {
            Response.Cookies["MessageList"].Value = ((LinkButton)sender).CommandArgument;
            Response.Redirect("MessageList.aspx");
        }
        //vraci jmeno tymu
        protected string GetTeamName()
        {
            return MidApp.ManageTeam.GetTeamByID(TeamID).TeamName;
        }
        //obsluha pro presmerovani na detail tymu
        protected void LinkButtonTeamInfo_Click(object sender, EventArgs e)
        {
            Response.Cookies["TeamDetail"].Value = TeamID.ToString();
            Response.Redirect("TeamDetails.aspx");
        }
        protected string GetTeamID()
        {
            return TeamID.ToString();
        }
        protected string GetTeamAvatar()
        {
            var aux = MidApp.ManageTeam.GetTeamByID(TeamID).ImageURL;
            
            return aux;

        }
        protected string DateOfLastPost(object sender)
        {
            var aux = sender as AppLib.Topic;
            AppLib.Message date=null;
            var dat="";

            try
            {
                date = MidApp.ManageMessage.GetMessagesAssignedToTopic(aux.ThreadID).First();
                dat = date.Inserted.Value.ToShortDateString() +" "+ date.Inserted.Value.ToShortTimeString();

            }
            catch (Exception e)
            {
                e.ToString();
                dat = "Prázdné téma.";
            }
            return dat;
        }
        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            Response.Redirect("ForumList.aspx");
        }
    }
}